package com.example.ospswagger.config;

import com.example.ospswagger.filter.EncodingFilter;
import com.example.ospswagger.servlet.SwaggerServlet;
import io.swagger.servlet.config.DefaultServletConfig;
import io.swagger.servlet.listing.ApiDeclarationServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: lfc
 * @version: 2020/5/15 22:09
 */
//@Configuration
public class ServletConfig {
    /**
     * @return
     */
    @Bean
    public ServletRegistrationBean swaggerServlet() {
        ServletRegistrationBean registrationBean = new ServletRegistrationBean();
        registrationBean.setServlet(new SwaggerServlet());
        registrationBean.setName("SwaggerServlet");
        registrationBean.addUrlMappings("/SwaggerServlet");
        return registrationBean;
    }

    @Bean
    public ServletRegistrationBean apiDeclarationServlet() {
        ServletRegistrationBean registrationBean = new ServletRegistrationBean();
        registrationBean.setServlet(new ApiDeclarationServlet());
        registrationBean.setName("ApiDeclarationServlet");
        registrationBean.addUrlMappings("/docs/*");
        return registrationBean;
    }

    @Bean
    public ServletRegistrationBean swaggerDefaultReadServlet() {
        ServletRegistrationBean registrationBean = new ServletRegistrationBean();
        registrationBean.setServlet(new DefaultServletConfig());
        Map<String,String> initParams = new HashMap<String,String>();
        initParams.put("swagger.resource.package","com.example.ospswagger");
        initParams.put("swagger.api.basepath","/demo");
        initParams.put("api.version","1");
        registrationBean.setInitParameters(initParams);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/init");
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean encodingFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new EncodingFilter());
        Map<String,String> initParams = new HashMap<String,String>();
        initParams.put("Encoding","UTF-8");
        registrationBean.setName("encodingFilter");
        registrationBean.addUrlPatterns("/*");
        return registrationBean;
    }
}
